/*
 * GUIObject.cpp
 *
 *  Created on: 20/10/2009
 *      Author: marcos
 */

#include "GUIObject.h"
namespace gui {

int toPixels(double v) {
	return util::doubleToInt(v * METERS_TO_PIXEL_RATIO);
}
const static RGBColor UNCOLORED = { -1, -1, -1 };

GUIObject::GUIObject(GUIObjectDrawer* drawer, std::string filename, RGBColor transparentColor) {
	this->xPos = 0;
	this->yPos = 0;
	this->rotation = 0;
	this->texture = filename;
	this->baseColor = BLACK;
	this->textureTransparentColor = transparentColor;
	this->hasTransparentColor = true;
	this->drawer = drawer;
	this->surface = NULL;
	redrawSurface();
}
GUIObject::GUIObject(GUIObjectDrawer* drawer, RGBColor baseColor) {
	this->xPos = 0;
	this->yPos = 0;
	this->rotation = 0;
	this->texture = "";
	this->baseColor = baseColor;
	this->textureTransparentColor = UNCOLORED;
	this->hasTransparentColor = false;
	this->drawer = drawer;
	this->surface = NULL;
	redrawSurface();
}

void GUIObject::redrawSurface() {
	this->surface = this->drawer->drawGUIObject(this);
}
GUIObject::~GUIObject() {
	if (this->surface != NULL) delete this->surface;
}

int GUIObject::getRColor() {
	return this->baseColor.red;
}

int GUIObject::getGColor() {
	return this->baseColor.green;
}

int GUIObject::getBColor() {
	return this->baseColor.blue;
}

int GUIObject::getXPos() {
	return this->xPos;
}

int GUIObject::getYPos() {
	return this->yPos;
}

double GUIObject::getRotation() {
	return this->rotation;
}

int GUIObject::getWidth() {
	return this->surface->getWidth();
}

int GUIObject::getHeight() {
	return this->surface->getHeight();
}

std::string GUIObject::getTexture() {
	return this->texture;
}

int GUIObject::getTextureTransparentColorR() {
	return this->textureTransparentColor.red;
}

int GUIObject::getTextureTransparentColorG() {
	return this->textureTransparentColor.green;
}

int GUIObject::getTextureTransparentColorB() {
	return this->textureTransparentColor.blue;
}

bool GUIObject::getTextureStrech() {
//	return this->textureStrech; XXX
	return false;
}
int GUIObject::getExpectedLength() {
	return this->expectedLength;
}
bool GUIObject::getTextureHasTransparentColor() {
	return this->hasTransparentColor;
}

void GUIObject::setBGColor(RGBColor baseColor) {
	this->baseColor = baseColor;
}

void GUIObject::setCenter(int x, int y) {
	if (surface != NULL) {
		this->surface->setCenter(x, y);
	}
	this->xPos = x;
	this->yPos = y;
}

void GUIObject::setRotation(double rotation) {
	if (surface != NULL) {
		this->surface->rotate(rotation);
	}
	this->rotation = rotation;
}

//void GUIObject::setTexture(std::string texture, int expectedLength) {
//	this->texture = texture;
//	this->textureStrech = expectedLength == -1;
//	this->hasTransparentColor = false;
//}
//void GUIObject::setTexture(std::string texture, RGBColor transparentColor, bool shouldStrech) {
//	this->setTexture(texture, shouldStrech);
//	this->hasTransparentColor = true;
//	this->textureTransparentColor = transparentColor;
//}
/* Recibe metros */
void GUIObject::setCenterInMeters(double x, double y) {
	this->setCenter(toPixels(x), toPixels(y));
}
void GUIObject::setSizeInMeters(double w, double h) {
	this->setSize(w == -1.0? -1 : toPixels(w), h == -1.0? -1 : toPixels(h));
}
void GUIObject::setSize(int w, int h) {
	w = (w == -1)? this->surface->getWidth()  : w;
	h = (h == -1)? this->surface->getHeight() : h;
	this->surface->resize(w,h);
}
Surface* GUIObject::asSurface() {
	return this->surface;
}
}
